<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\helper;

use builder\tree_table\HelperInterface;
use builder\base\InvalidInstanceException;
use builder\tree_table\util\TTTableHeadHelper;
use builder\tree_table\util\TTRowOperationHelper;
use builder\tree_table\util\TTToolbarCustomHelper;
use builder\tree_table\util\TTToolbarDropdownItemHelper;
use builder\tree_table\util\TTToolbarFilterHelper;
use builder\tree_table\util\TTToolbarTab;

/**
 * 树表格构建助手
 * @author cleverstone
 * @since ym2.0
 */
class TreeTable
{
    /**
     * @var HelperInterface
     */
    private static $_helper;

    /**
     * 表头助手实例
     * @return TTTableHeadHelper
     * @throws InvalidInstanceException
     */
    public static function treeTableHead()
    {
        return self::get(TTTableHeadHelper::class);
    }

    /**
     * 行操作助手实例
     * @return TTRowOperationHelper
     * @throws InvalidInstanceException
     */
    public static function ttRowOperation()
    {
        return self::get(TTRowOperationHelper::class);
    }

    /**
     * 自定义工具项助手实例
     * @return TTToolbarCustomHelper
     * @throws InvalidInstanceException
     */
    public static function ttToolbarCustom()
    {
        return self::get(TTToolbarCustomHelper::class);
    }

    /**
     * 自定义工具项下拉菜单选项构建助手实例
     * @return TTToolbarDropdownItemHelper
     * @throws InvalidInstanceException
     */
    public static function ttToolbarDropdown()
    {
        return self::get(TTToolbarDropdownItemHelper::class);
    }

    /**
     * 工具筛选项助手实例
     * @return TTToolbarFilterHelper
     * @throws InvalidInstanceException
     */
    public static function ttToolbarFilter()
    {
        return self::get(TTToolbarFilterHelper::class);
    }

    /**
     * 工具栏选项卡选项助手实例
     * @return TTToolbarTab
     */
    public static function ttToolbarTab()
    {
        return new TTToolbarTab();
    }

    /**
     * @param string $class 助手类名
     * @return HelperInterface
     * @throws InvalidInstanceException
     */
    public static function get($class)
    {
        if (!empty(self::$_helper[$class])) {
            return self::$_helper[$class];
        }

        $object = new $class();
        if (!$object instanceof HelperInterface) {
            throw new InvalidInstanceException($class . ' must be instance of ' . HelperInterface::class);
        }

        self::$_helper[$class] = $object;

        return $object;
    }
}